home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / adv_texture / reflection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.6 KB  |  200 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* reflection.c
  19.  * This program reads in a texture from a .rgb file, sets up the
  20.  * texture and the texture environment, then applies the texture
  21.  * to a polygon using sphere mapping to automatically generated 
  22.  * texture coordinates.
  23.  *
  24.  *    Escape Key    - exit program
  25.  */
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. #include <math.h>
  31. #include <stdio.h>
  32.  
  33. #include "rgbImageFile.h"    /* should be in ../../include */
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  animate( GLvoid );
  39. GLvoid  visibility( GLint );
  40. GLvoid  drawScene( GLvoid );
  41. GLvoid  reshape( GLsizei, GLsizei );
  42. GLvoid  keyboard( GLubyte, GLint, GLint );
  43.  
  44. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  45.  
  46. void  printHelp( char *progname );
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. /* Global Variables */
  53.  
  54. static GLfloat spin = 1.0;
  55.  
  56. void
  57. main( int argc, char *argv[] )
  58. {
  59.     char        *imageFileName = "flowers.rgb";
  60.     unsigned int     *image;
  61.     GLsizei        width, height, imageWidth, imageHeight;
  62.     
  63.     glutInit( &argc, argv );
  64.  
  65.     if (argc < 2) {
  66.         fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
  67.     } else
  68.         imageFileName = argv[1];
  69.  
  70.     fprintf( stdout, "using image %s\n", imageFileName );
  71.  
  72.     image = rgbReadImageFile(imageFileName, &imageWidth,
  73.              &imageHeight);
  74.  
  75.     /* create a window that is 1/4 the size of the screen */
  76.  
  77.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  78.     height = glutGet( GLUT_SCREEN_HEIGHT );
  79.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  80.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  81.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  82.     glutCreateWindow( argv[0] );
  83.  
  84.     initTexture( image, imageWidth, imageHeight );
  85.     initgfx();
  86.  
  87.     glutKeyboardFunc( keyboard );
  88.     glutIdleFunc( animate );
  89.     glutVisibilityFunc( visibility );
  90.     glutReshapeFunc( reshape );
  91.     glutDisplayFunc( drawScene ); 
  92.  
  93.     printHelp( argv[0] );
  94.  
  95.     glutMainLoop();
  96. }
  97.  
  98. GLvoid
  99. printHelp( char *progname )
  100. {
  101.     fprintf(stdout, "\n%s - demonstrates reflection mapping\n"
  102.         "Escape key     - exit the program\n\n",
  103.         progname );
  104. }
  105.  
  106. GLvoid
  107. initgfx( void )
  108. {
  109.     glEnable( GL_DEPTH_TEST );
  110.  
  111.     glClearColor( 0, 0, 0, 1 );
  112. }
  113.  
  114. GLvoid initTexture( unsigned int *image, 
  115.     GLsizei imageWidth, GLsizei imageHeight )
  116. {
  117.     /* scale texture image, make mipmaps and load texture
  118.      *    gluBuild2DMipmaps( target, components, width, height,
  119.      *        format,  type, imageArray ) 
  120.      */
  121.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  122.              GL_RGBA, GL_UNSIGNED_BYTE, image );
  123.                         
  124.     /* Set texture coordinate generation function for both
  125.      * the s and t planes to sphere map mode.  Texture
  126.      * will appear to reflect the environment.
  127.      */
  128.     glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
  129.     glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
  130.  
  131.     /* Enable automatic texture coordinate generation for 
  132.      * both the s and t planes
  133.      */
  134.     glEnable( GL_TEXTURE_GEN_S );
  135.     glEnable( GL_TEXTURE_GEN_T );
  136.  
  137.     glEnable( GL_TEXTURE_2D );
  138. }
  139.  
  140. GLvoid 
  141. keyboard( GLubyte key, GLint x, GLint y )
  142. {
  143.     switch (key) {
  144.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  145.         exit(0);
  146.     }
  147. }
  148.  
  149. GLvoid 
  150. animate( GLvoid )
  151. {
  152.     spin = fmodf( spin + 3.0, 360.0 );
  153.  
  154.     /* Tell GLUT to redraw the scene */
  155.     glutPostRedisplay();
  156. }
  157.  
  158. GLvoid
  159. visibility( int state ) 
  160. {
  161.     if (state == GLUT_VISIBLE) {
  162.         glutIdleFunc( animate );
  163.     } else {
  164.         glutIdleFunc( NULL );
  165.     }
  166. }
  167.  
  168. GLvoid
  169. reshape( GLsizei width, GLsizei height )
  170. {
  171.     GLdouble     aspect;
  172.  
  173.     glViewport( 0, 0, width, height );
  174.  
  175.     aspect = (GLdouble) width / (GLdouble) height;
  176.  
  177.     glMatrixMode( GL_PROJECTION );
  178.     glLoadIdentity();
  179.     gluPerspective( 45.0, aspect, 3.0, 15.0 );
  180.     glMatrixMode( GL_MODELVIEW );
  181.     glLoadIdentity();
  182.     glTranslatef( 0.0, 0.0, -12.0 ); 
  183. }
  184.  
  185. GLvoid
  186. drawScene(void)
  187. {
  188.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  189.  
  190.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  191.     glPushMatrix ();
  192.         glRotatef(spin + 120.0, 0.0, 1.0, 0.0);
  193.         glTranslatef(3.0, 0.0, 0.0);
  194.         glutSolidSphere( 1.0, 32, 32 );
  195.     glPopMatrix ();
  196.  
  197.     glutSwapBuffers();
  198. }
  199.  
  200.